/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ies.domain.model.web;

import com.ies.domain.model.Cidade;
import com.ies.domain.model.Endereco;
import com.ies.domain.model.Permissao;
import com.ies.domain.model.Usuario;
import com.ies.domain.model.Usuario.TipoUsuario;
import com.ies.domain.model.repository.facades.CidadeFacade;
import com.ies.domain.model.repository.facades.PermissaoFacade;
import com.ies.domain.model.repository.facades.UsuarioFacade;
import com.ies.domain.model.util.JpaPool;
import com.ies.domain.model.util.ModelInterface;
import com.ies.domain.model.web.converter.PermissaoConverter;
import com.ies.domain.model.web.util.AbstractCrudManagedBean;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

/**
 *
 * @author jackson
 */
@ManagedBean(name = "UsuarioCrudMB")
@ViewScoped
public class UsuarioCrudMB extends AbstractCrudManagedBean<Usuario> {

    
    private Endereco endereco;
    private List<String> permissoesUser;

    @Override
    public void init() {
        entity = new Usuario();
        entity.setEndereco(new Endereco());
        permissoesUser = new ArrayList<String>();
    }

    @Override
    public Usuario getEntity() {
        if (entity == null) {
            init();
        }
        if (getRequestParam("id") != null && !getRequestParam("id").isEmpty()) {
            if (((ModelInterface) entity).getId() == null || ((ModelInterface) entity).getId() != Long.parseLong(getRequestParam("id"))) {
                entity = (Usuario) JpaPool.getInstancia().getFacade(entity.getClass()).find((Long.parseLong(getRequestParam("id"))));
                if (entity.getPermissaos() != null) {
                    PermissaoConverter converter = new PermissaoConverter();
                    for (Permissao permissao : entity.getPermissaos()) {
                        permissoesUser.add(converter.getAsString(null, null, permissao));
                    }
                }
            }
        }
        return entity;
    }

    @Override
    public String save() {

        if (entity.getId() == null) {
            entity.setDataCadastro(new Date());
        }
        if (entity.getEndereco() == null) {
            entity.setEndereco(new Endereco());
        }
        if (getPermissoesUser() != null) {
            PermissaoFacade facade = (PermissaoFacade) JpaPool.get(Permissao.class);
            List<Permissao> list = new ArrayList<Permissao>();
            for (String objeto : getPermissoesUser()) {
                if (objeto != null) {
                    String id = objeto.replaceAll("\\D", "");
                    Permissao perm = facade.find(Long.parseLong(id));
                    list.add(perm);
                }
            }
            entity.setPermissaos(list);
        }
        
        return super.save();

    }

    
    
    
    public List<SelectItem> permissoes() {
        List<SelectItem> lista = new ArrayList<SelectItem>();
        PermissaoFacade facade = (PermissaoFacade) JpaPool.get(Permissao.class);
        List<Permissao> permissoes = facade.findAll();
        if (permissoes != null) {
            for (Permissao p : permissoes) {
                lista.add(new SelectItem(p, p.getNome()));
            }
        }
        return lista;

    }

    public List<SelectItem> getCidades() {
        List<SelectItem> items = new ArrayList<SelectItem>();
        CidadeFacade cf = (CidadeFacade) JpaPool.get(Cidade.class);
        List<Cidade> cidades = cf.findAll();
        if (cidades != null) {
            for (Cidade cidade : cidades) {
                items.add(new SelectItem(cidade, cidade.getNome()));
            }
        }
        return items;
    }

    public List<SelectItem> getSexos() {
        List<SelectItem> items = new ArrayList<SelectItem>();
        for (Usuario.Sexo sexo : Usuario.Sexo.values()) {
            items.add(new SelectItem(sexo, sexo.toString()));
        }
        return items;
    }

    public List<SelectItem> getTipoUsuario() {
        List<SelectItem> items = new ArrayList<SelectItem>();
        for (Usuario.TipoUsuario user : Usuario.TipoUsuario.values()) {
            items.add(new SelectItem(user, user.toString()));
        }
        return items;
    }

    public Endereco getEndereco() {
        return endereco;
    }

    public void setEndereco(Endereco endereco) {
        this.endereco = endereco;
    }

    public List<String> getPermissoesUser() {
        return permissoesUser;
    }

    public void setPermissoesUser(List<String> permissoesUser) {
        this.permissoesUser = permissoesUser;
    }
}
